﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;

namespace MuraImage
{
    public partial class Form_SimulatePic : Form
    {
        //图像宽度和高度
        HTuple hv_ImageHeight,hv_ImageWidth;
        //背景灰阶
        HTuple hv_backGray;
        //缺陷个数
        int m_defectNum;
        #region 缺陷信息定义
        //灰阶差
        HTuple hv_grayDiff;
        //模拟的缺陷图片
        HObject ho_defect;
        //缺陷区域
        HObject ho_region;
        //缺陷半径
        HTuple hv_radius;
        //
        string filePath;
        HTuple hv_rowCenter, hv_colCenter;
        #endregion
        public Form_SimulatePic()
        {
            InitializeComponent();
            dataGridView_defectInfo.Rows[0].HeaderCell.Value = "1";
            dataGridView_defectInfo.AllowUserToAddRows = false;
            filePath = "D:\\halconspace";
            

        }
        private void Form_SimulatePic_Load(object sender, EventArgs e)
        {
            //BrightOrDark.Visible = true;
            //GrayDistr.Visible = true;
            //PositionX.Visible = true;
            //PositionY.Visible = true;
            //Radius.Visible = true;
            //Area.Visible = true;
            //DefectHeight.Visible = false;
            //DefectWidth.Visible = false;
            //Orition.Visible = false;
            comboBox_defectType.SelectedIndex = 0;
            comboBox_Resolution.SelectedIndex = 0;
            comboBox_defectShape.SelectedIndex = 0;
            textBox_backGray.Text = "255";
            textBox_defectNum.Text = "1";
            comboBox_PicNum.SelectedIndex = 0;
            comboBox_ChangeType.SelectedIndex = -1;
            textBox_stepGray.Text = "5";
            textBox_MinGray.Text = "5";
            textBox_MaxGray.Text = "255";
            textBox_stepRadius.Text = "2";
            textBox_MinRadius.Text = "2";
            textBox_MaxRadius.Text = "80";
            //hv_ImageHeight = 1920;
            //hv_ImageWidth = 1080;
            hv_backGray = int.Parse(textBox_backGray.Text);
            HOperatorSet.GenEmptyObj(out ho_defect);
            HOperatorSet.GenImageConst(out ho_defect, "byte", hv_ImageWidth, hv_ImageHeight);
            HOperatorSet.GenImageProto(ho_defect, out ho_defect, hv_backGray);
            
                
            

        }
        #region 控件定义
        //模拟图片分辨率
        private void comboBox_Resolution_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox_Resolution.SelectedIndex)
            {
                case 0:
                    hv_ImageWidth = 1080;
                    hv_ImageHeight = 1920;
                    break;
                case 1:
                    hv_ImageWidth = 1440;
                    hv_ImageHeight = 2560;
                    break;
                default:
                    break;
            }
        }
        //模拟图片缺陷形状
        private void comboBox_defectShape_SelectedIndexChanged(object sender, EventArgs e)
        {
            BrightOrDark.Visible = true;
            GrayDistr.Visible = true;
            PositionX.Visible = true;
            PositionY.Visible = true;
            transOrient.Visible = false;
            switch (comboBox_defectShape.SelectedIndex)
            {
                    //circle
                case 0:
                    grayDiff.Visible = true;
                    Radius.Visible = true;
                    DefectHeight.Visible = false;
                    DefectWidth.Visible = false;
                    Orition.Visible = false;
                    break;
                    //rectangle1
                case 1:
                    Radius.Visible = false;
                    DefectHeight.Visible = true;
                    DefectWidth.Visible = true;
                    Orition.Visible = false;
                    break;
                    //rectangle2
                case 2:
                    Radius.Visible = false;
                    DefectHeight.Visible = true;
                    DefectWidth.Visible = true;
                    Orition.Visible = true;
                    break;
                    //wholeimage
                case 3:
                    transOrient.Visible = true;
                    Radius.Visible = false;
                    DefectHeight.Visible = false;
                    DefectWidth.Visible = false;
                    Orition.Visible = false;
                    break;
                default:
                    break; 
            }
        }
        //模拟图片缺陷数量
        private void textBox_defectNum_TextChanged(object sender, EventArgs e)
        {
            m_defectNum = int.Parse(textBox_defectNum.Text);
            dataGridView_defectInfo.RowCount = m_defectNum;
            for (int i = 0; i < m_defectNum; i++)
            {
                dataGridView_defectInfo.Rows[i].Cells[GrayDistr.Index].Value = "高斯分布";
                dataGridView_defectInfo.Rows[i].Cells[PositionX.Index].Value = "600";
                dataGridView_defectInfo.Rows[i].Cells[PositionY.Index].Value = "800";
                dataGridView_defectInfo.Rows[i].Cells[BrightOrDark.Index].Value = "暗斑";
                dataGridView_defectInfo.Rows[i].Cells[grayDiff.Index].Value = "15";
                dataGridView_defectInfo.Rows[i].Cells[Radius.Index].Value = "50";
                dataGridView_defectInfo.Rows[i].Cells[DefectHeight.Index].Value = "50";
                dataGridView_defectInfo.Rows[i].Cells[DefectWidth.Index].Value = "100";
                dataGridView_defectInfo.Rows[i].Cells[Orition.Index].Value = "5";
                dataGridView_defectInfo.Rows[i].Cells[transOrient.Index].Value = "水平变化";
            }

        }
        //模拟缺陷图片数量变化
        private void comboBox_PicNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox_PicNum.SelectedIndex)
            {
                    //单张
                case 0:
                    grayDiff.Visible = true;
                    comboBox_ChangeType.SelectedIndex = -1;
                    comboBox_ChangeType.Visible = false;
                    label_sizeOrGray.Visible = false;

                    label_MinGray.Visible = false;
                    label_MaxGray.Visible = false;
                    label_stepGray.Visible = false;
                    textBox_MinGray.Visible = false;
                    textBox_MaxGray.Visible = false;
                    textBox_stepGray.Visible = false;

                    label_MinRadius.Visible = false;
                    label_MaxRadius.Visible = false;
                    label_stepRadius.Visible = false;
                    textBox_MinRadius.Visible = false;
                    textBox_MaxRadius.Visible = false;
                    textBox_stepRadius.Visible = false;
                    break;
                    //多张
                case 1:
                    comboBox_ChangeType.Visible = true;
                    label_sizeOrGray.Visible = true;
                    comboBox_ChangeType.SelectedIndex = 0;
                    break;
                default:
                    break;
            }
        }
        //模拟缺陷图片变化类型(灰阶差or半径变化)
        private void comboBox_ChangeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox_ChangeType.SelectedIndex)
            {
                    //灰阶变化
                case 0:
                    Radius.Visible = true;
                    grayDiff.Visible = false;
                    label_MinGray.Visible = true;
                    label_MaxGray.Visible = true;
                    label_stepGray.Visible = true;
                    textBox_MinGray.Visible = true;
                    textBox_MaxGray.Visible = true;
                    textBox_stepGray.Visible = true;

                    label_MinRadius.Visible = false;
                    label_MaxRadius.Visible = false;
                    label_stepRadius.Visible = false;
                    textBox_MinRadius.Visible = false;
                    textBox_MaxRadius.Visible = false;
                    textBox_stepRadius.Visible = false;
                    break;
                    //大小变化
                case 1:
                    grayDiff.Visible = true;
                    Radius.Visible = false;
                    label_MinGray.Visible = false;
                    label_MaxGray.Visible = false;
                    label_stepGray.Visible = false;
                    textBox_MinGray.Visible = false;
                    textBox_MaxGray.Visible = false;
                    textBox_stepGray.Visible = false;

                    label_MinRadius.Visible = true;
                    label_MaxRadius.Visible = true;
                    label_stepRadius.Visible = true;
                    textBox_MinRadius.Visible = true;
                    textBox_MaxRadius.Visible = true;
                    textBox_stepRadius.Visible = true;
                    break;
                default:
                    break;
            }
        }
        //模拟缺陷类型
        private void comboBox_defectType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox_defectType.SelectedIndex)
            {
                //斑
                case 0:
                    comboBox_defectShape.SelectedIndex = 0;
                    break;
                //条纹
                case 1:
                    comboBox_defectShape.SelectedIndex = 1;
                    break;
                //颜色均匀性
                case 2:
                    comboBox_defectShape.SelectedIndex = 3;
                    break;
                //亮度均匀性
                case 3:
                    comboBox_defectShape.SelectedIndex = 3;
                    break;
                default:
                    break;

            }


        }
        #endregion
        private void button_genImage_Click(object sender, EventArgs e)
        {
            HTuple hv_minGray, hv_maxGray, hv_stepGray;
            HTuple hv_minRadius, hv_maxRadius, hv_stepRadius;
            switch (comboBox_PicNum.SelectedIndex)
            {
                case 0:
                    gen_one_image();
                    HOperatorSet.WriteImage(ho_defect, "bmp", 0, filePath + "\\1.bmp");
                    break;
                case 1:
                    switch (comboBox_ChangeType.SelectedIndex)
                    {
                        case 0:
                            hv_minGray = int.Parse(textBox_MinGray.Text);
                            hv_maxGray = int.Parse(textBox_MaxGray.Text);
                            hv_stepGray = int.Parse(textBox_stepGray.Text);
                            for (int picIndex = 0; picIndex <= (hv_maxGray-hv_minGray) / hv_stepGray;picIndex++ )
                            {
                                hv_grayDiff =hv_minGray+ picIndex * hv_stepGray;
                                gen_image_varyGray(hv_grayDiff,out ho_defect);
                                HOperatorSet.WriteImage(ho_defect, "bmp", 0, filePath + "\\"+picIndex+".bmp");
                            }
                            
                            break;
                        case 1:
                            hv_minRadius = int.Parse(textBox_MinRadius.Text);
                            hv_maxRadius = int.Parse(textBox_MaxRadius.Text);
                            hv_stepRadius = int.Parse(textBox_stepRadius.Text);
                            for (int picIndex = 0; picIndex <= (hv_maxRadius - hv_minRadius) / hv_stepRadius; picIndex++)
                            {
                                hv_radius = hv_minRadius + picIndex * hv_stepRadius;
                                gen_image_varySize(hv_radius, out ho_defect);
                                HOperatorSet.WriteImage(ho_defect, "bmp", 0, filePath + "\\" + picIndex + ".bmp");
                            }
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
            }
            MessageBox.Show("success");
        }
        public void gen_one_image()
        {
            HObject ho_tempImage;
            HTuple hv_row1, hv_row2, hv_col1, hv_col2, hv_foreGray;
            HTuple hv_defectHeight, hv_defectWidth;
            //方向角
            HTuple hv_phi;
            //区域内点坐标
            HTuple hv_rowsP, hv_colsP;
            HTuple hv_length;
            //高斯参数
            HTuple hv_sigma = 0;
            HTuple hv_gauss;
            HTuple hv_step, hv_Sequence;
            hv_backGray = int.Parse(textBox_backGray.Text);
            HOperatorSet.GenEmptyObj(out ho_tempImage);
            HOperatorSet.GenImageConst(out ho_tempImage, "byte", hv_ImageWidth, hv_ImageHeight);
            HOperatorSet.GenImageProto(ho_tempImage, out ho_tempImage, hv_backGray);
            //HOperatorSet.DispObj(ho_defect, hWindowControl1.HalconWindow);
            for (int i = 0; i < m_defectNum; i++)
            {

                hv_colCenter = int.Parse(dataGridView_defectInfo[PositionY.Index, i].EditedFormattedValue.ToString());
                hv_rowCenter = int.Parse(dataGridView_defectInfo[PositionX.Index, i].EditedFormattedValue.ToString());
                hv_radius = int.Parse(dataGridView_defectInfo[Radius.Index, i].EditedFormattedValue.ToString());
                hv_defectHeight = int.Parse(dataGridView_defectInfo[DefectHeight.Index, i].EditedFormattedValue.ToString());
                hv_defectWidth = int.Parse(dataGridView_defectInfo[DefectWidth.Index, i].EditedFormattedValue.ToString());
                hv_phi = int.Parse(dataGridView_defectInfo[Orition.Index, i].EditedFormattedValue.ToString());
                hv_grayDiff = int.Parse(dataGridView_defectInfo[grayDiff.Index, i].EditedFormattedValue.ToString());
                #region 缺陷区域定义
                switch (comboBox_defectShape.SelectedIndex)
                {
                    //circle
                    case 0:
                        hv_sigma = hv_radius / 2;
                        HOperatorSet.GenCircle(out ho_region, hv_rowCenter, hv_colCenter, hv_radius);
                        break;
                    //rectangle1
                    case 1:
                        hv_sigma = hv_defectHeight / 2;
                        hv_row1 = hv_rowCenter - hv_defectHeight / 2;
                        hv_row2 = hv_rowCenter + hv_defectHeight / 2;
                        hv_col1 = hv_colCenter - hv_defectWidth / 2;
                        hv_col2 = hv_colCenter + hv_defectWidth / 2;
                        HOperatorSet.GenRectangle1(out ho_region, hv_row1, hv_col1, hv_row2, hv_col2);
                        break;
                    //rectangle2
                    case 2:
                        hv_sigma = hv_defectWidth / 2;
                        HOperatorSet.GenRectangle2(out ho_region, hv_rowCenter, hv_colCenter, hv_phi, hv_defectHeight, hv_defectWidth);
                        break;
                    //wholeImage
                    case 3:
                        hv_sigma = hv_ImageHeight / 2;
                        HOperatorSet.GenRectangle1(out ho_region, 0, 0, hv_ImageHeight, hv_ImageWidth);
                        break;
                    default:
                        break;
                }
                #endregion
                HOperatorSet.GetRegionPoints(ho_region, out hv_rowsP, out hv_colsP);
                HOperatorSet.TupleLength(hv_rowsP, out hv_length);
                if (dataGridView_defectInfo[BrightOrDark.Index, i].EditedFormattedValue.ToString() == "暗斑")
                {
                    hv_grayDiff = -hv_grayDiff;
                }
                #region 灰阶分布类型
                if (dataGridView_defectInfo[GrayDistr.Index, i].EditedFormattedValue.ToString() == "均匀分布")
                {
                    HOperatorSet.TupleGenConst(hv_length, hv_grayDiff, out hv_grayDiff);
                }
                else if (dataGridView_defectInfo[GrayDistr.Index, i].EditedFormattedValue.ToString() == "高斯分布")
                {
                    HOperatorSet.TupleGenConst(hv_length, hv_rowCenter, out hv_rowCenter);
                    HOperatorSet.TupleGenConst(hv_length, hv_colCenter, out hv_colCenter);
                    //hv_gauss=Math.Exp((-(Math.Pow(hv_rowsP-hv_rowCenter,2)+Math.Pow(hv_colsP-hv_colCenter,2)))/(2*Math.Pow(hv_sigma,2)));
                    hv_gauss = (((-((((hv_rowsP - hv_rowCenter)).TuplePow(2)) + (((hv_colsP - hv_colCenter)).TuplePow(2)))) / (2 * (hv_sigma.TuplePow(2))))).TupleExp();
                    hv_grayDiff = hv_grayDiff * hv_gauss;
                }
                else if (dataGridView_defectInfo[GrayDistr.Index, i].EditedFormattedValue.ToString() == "锥形分布")
                {
                    hv_step = hv_grayDiff / hv_length;
                    tuple_gen_sequence(0, hv_length, hv_step, out hv_Sequence);
                    hv_grayDiff = hv_grayDiff * hv_Sequence;
                }
                #endregion
                hv_foreGray = hv_grayDiff + hv_backGray;
                HOperatorSet.SetGrayval(ho_tempImage, hv_rowsP, hv_colsP, hv_foreGray);
            }

            HOperatorSet.Compose3(ho_tempImage, ho_tempImage, ho_tempImage, out ho_defect);
            HOperatorSet.DispObj(ho_defect, hWindowControl1.HalconWindow);

        }
        public void gen_image_varyGray(HTuple hv_grayDiff,out HObject ho_defect)
        {
            HObject ho_tempImage;
            HTuple hv_row1, hv_row2, hv_col1, hv_col2,hv_foreGray;
            HTuple hv_defectHeight, hv_defectWidth;
            //方向角
            HTuple hv_phi;
            //区域内点坐标
            HTuple hv_rowsP,hv_colsP;
            HTuple hv_length;
            //高斯参数
            HTuple hv_sigma=0;
            HTuple hv_gauss;
            HTuple hv_step,hv_Sequence;
            hv_backGray = int.Parse(textBox_backGray.Text);
            HOperatorSet.GenEmptyObj(out ho_tempImage);
            HOperatorSet.GenImageConst(out ho_tempImage, "byte", hv_ImageWidth, hv_ImageHeight);
            HOperatorSet.GenImageProto(ho_tempImage, out ho_tempImage, hv_backGray);
            //HOperatorSet.DispObj(ho_defect, hWindowControl1.HalconWindow);
            for (int i = 0; i < m_defectNum; i++)
            {
                
                hv_colCenter = int.Parse(dataGridView_defectInfo[PositionY.Index, i].EditedFormattedValue.ToString());
                hv_rowCenter = int.Parse(dataGridView_defectInfo[PositionX.Index, i].EditedFormattedValue.ToString());
                hv_radius = int.Parse(dataGridView_defectInfo[Radius.Index, i].EditedFormattedValue.ToString());
                hv_defectHeight = int.Parse(dataGridView_defectInfo[DefectHeight.Index, i].EditedFormattedValue.ToString());
                hv_defectWidth = int.Parse(dataGridView_defectInfo[DefectWidth.Index, i].EditedFormattedValue.ToString());
                hv_phi = int.Parse(dataGridView_defectInfo[Orition.Index, i].EditedFormattedValue.ToString());
                #region 缺陷区域定义
                switch (comboBox_defectShape.SelectedIndex)
                {
                        //circle
                    case 0:
                        hv_sigma = hv_radius / 2;
                        HOperatorSet.GenCircle(out ho_region, hv_rowCenter, hv_colCenter, hv_radius);
                        break;
                        //rectangle1
                    case 1:
                        hv_sigma = hv_defectHeight / 2;
                        hv_row1 = hv_rowCenter - hv_defectHeight / 2;
                        hv_row2 = hv_rowCenter + hv_defectHeight / 2;
                        hv_col1 = hv_colCenter - hv_defectWidth / 2;
                        hv_col2 = hv_colCenter + hv_defectWidth / 2;
                        HOperatorSet.GenRectangle1(out ho_region, hv_row1, hv_col1, hv_row2, hv_col2);
                        break;
                        //rectangle2
                    case 2:
                        hv_sigma = hv_defectWidth / 2;
                        HOperatorSet.GenRectangle2(out ho_region, hv_rowCenter, hv_colCenter, hv_phi, hv_defectHeight, hv_defectWidth);
                        break;
                        //wholeImage
                    case 3:
                        hv_sigma = hv_ImageHeight / 2;
                        HOperatorSet.GenRectangle1(out ho_region, 0, 0, hv_ImageHeight, hv_ImageWidth);
                        break;
                    default:
                        break;
                }
                #endregion
                HOperatorSet.GetRegionPoints(ho_region, out hv_rowsP, out hv_colsP);
                HOperatorSet.TupleLength(hv_rowsP, out hv_length);
                if (dataGridView_defectInfo[BrightOrDark.Index, i].EditedFormattedValue.ToString() == "暗斑")
                {
                    hv_grayDiff = -hv_grayDiff;
                }
                #region 灰阶分布类型
                if (dataGridView_defectInfo[GrayDistr.Index, i].EditedFormattedValue.ToString() == "均匀分布")
                {
                    HOperatorSet.TupleGenConst(hv_length, hv_grayDiff, out hv_grayDiff);
                }
                else if (dataGridView_defectInfo[GrayDistr.Index, i].EditedFormattedValue.ToString() == "高斯分布")
                {
                    HOperatorSet.TupleGenConst(hv_length, hv_rowCenter, out hv_rowCenter);
                    HOperatorSet.TupleGenConst(hv_length, hv_colCenter, out hv_colCenter);
                    //hv_gauss=Math.Exp((-(Math.Pow(hv_rowsP-hv_rowCenter,2)+Math.Pow(hv_colsP-hv_colCenter,2)))/(2*Math.Pow(hv_sigma,2)));
                    hv_gauss = (((-((((hv_rowsP - hv_rowCenter)).TuplePow(2)) + (((hv_colsP - hv_colCenter)).TuplePow(2)))) / (2 * (hv_sigma.TuplePow(2))))).TupleExp();
                    hv_grayDiff = hv_grayDiff * hv_gauss;
                }
                else if (dataGridView_defectInfo[GrayDistr.Index, i].EditedFormattedValue.ToString() == "锥形分布")
                {
                    hv_step=hv_grayDiff/hv_length;
                    tuple_gen_sequence(0,hv_length,hv_step,out hv_Sequence);
                    hv_grayDiff=hv_grayDiff*hv_Sequence;
                }
                #endregion
                
                #region 缺陷类型定义
                //switch (comboBox_defectType.SelectedIndex)
                //{
                //        //斑
                //    case 0:
                        
                //        break;
                //        //条纹
                //    case 1:
                //        break;
                //        //颜色均匀性
                //    case 2:
                //        break;
                //        //亮度均匀性
                //    case 3:
                //        break;
                //    default:
                //        break;
                //}
                #endregion
                hv_foreGray = hv_grayDiff + hv_backGray;
                HOperatorSet.SetGrayval(ho_tempImage, hv_rowsP, hv_colsP, hv_foreGray);
            }

            HOperatorSet.Compose3(ho_tempImage, ho_tempImage, ho_tempImage, out ho_defect);
            HOperatorSet.DispObj(ho_defect, hWindowControl1.HalconWindow);
            
        }
        public void gen_image_varySize(HTuple hv_radius,out HObject ho_defect)
        {
            HObject ho_tempImage;
            HTuple hv_foreGray;
            HTuple hv_defectHeight, hv_defectWidth;
            //方向角
            HTuple hv_phi;
            //区域内点坐标
            HTuple hv_rowsP, hv_colsP;
            HTuple hv_length;
            //高斯参数
            HTuple hv_sigma = 0;
            HTuple hv_gauss;
            HTuple hv_step, hv_Sequence;
            hv_backGray = int.Parse(textBox_backGray.Text);
            hv_sigma = hv_radius / 2;
            HOperatorSet.GenEmptyObj(out ho_tempImage);
            HOperatorSet.GenImageConst(out ho_tempImage, "byte", hv_ImageWidth, hv_ImageHeight);
            HOperatorSet.GenImageProto(ho_tempImage, out ho_tempImage, hv_backGray);
            //HOperatorSet.DispObj(ho_defect, hWindowControl1.HalconWindow);
            for (int i = 0; i < m_defectNum; i++)
            {

                hv_colCenter = int.Parse(dataGridView_defectInfo[PositionY.Index, i].EditedFormattedValue.ToString());
                hv_rowCenter = int.Parse(dataGridView_defectInfo[PositionX.Index, i].EditedFormattedValue.ToString());
                hv_defectHeight = int.Parse(dataGridView_defectInfo[DefectHeight.Index, i].EditedFormattedValue.ToString());
                hv_defectWidth = int.Parse(dataGridView_defectInfo[DefectWidth.Index, i].EditedFormattedValue.ToString());
                hv_phi = int.Parse(dataGridView_defectInfo[Orition.Index, i].EditedFormattedValue.ToString());
                hv_grayDiff = int.Parse(dataGridView_defectInfo[grayDiff.Index, i].EditedFormattedValue.ToString());
                
                HOperatorSet.GenCircle(out ho_region, hv_rowCenter, hv_colCenter, hv_radius);
                HOperatorSet.GetRegionPoints(ho_region, out hv_rowsP, out hv_colsP);
                HOperatorSet.TupleLength(hv_rowsP, out hv_length);
                if (dataGridView_defectInfo[BrightOrDark.Index, i].EditedFormattedValue.ToString() == "暗斑")
                {
                    hv_grayDiff = -hv_grayDiff;
                }
                #region 灰阶分布类型
                if (dataGridView_defectInfo[GrayDistr.Index, i].EditedFormattedValue.ToString() == "均匀分布")
                {
                    HOperatorSet.TupleGenConst(hv_length, hv_grayDiff, out hv_grayDiff);
                }
                else if (dataGridView_defectInfo[GrayDistr.Index, i].EditedFormattedValue.ToString() == "高斯分布")
                {
                    //hv_gauss=Math.Exp((-(Math.Pow(hv_rowsP-hv_rowCenter,2)+Math.Pow(hv_colsP-hv_colCenter,2)))/(2*Math.Pow(hv_sigma,2)));
                    hv_gauss = (((-((((hv_rowsP - hv_rowCenter)).TuplePow(2)) + (((hv_colsP - hv_colCenter)).TuplePow(2)))) / (2 * (hv_sigma.TuplePow(2))))).TupleExp();
                    hv_grayDiff = hv_grayDiff * hv_gauss;
                }
                else if (dataGridView_defectInfo[GrayDistr.Index, i].EditedFormattedValue.ToString() == "锥形分布")
                {
                    hv_step = hv_grayDiff / hv_length;
                    tuple_gen_sequence(0, hv_length, hv_step, out hv_Sequence);
                    hv_grayDiff = hv_grayDiff * hv_Sequence;
                }
                #endregion
                hv_foreGray = hv_grayDiff + hv_backGray;
                HOperatorSet.SetGrayval(ho_tempImage, hv_rowsP, hv_colsP, hv_foreGray);
            }

            HOperatorSet.Compose3(ho_tempImage, ho_tempImage, ho_tempImage, out ho_defect);
            HOperatorSet.DispObj(ho_defect, hWindowControl1.HalconWindow);

        }
        public void tuple_gen_sequence(HTuple hv_Start, HTuple hv_End, HTuple hv_Step,out HTuple hv_Sequence)
        {
            //
            hv_Sequence = (hv_Start - hv_Step) + ((HTuple.TupleGenConst(((((hv_End - hv_Start) / hv_Step)).TupleInt()
                ) + 1, hv_Step)).TupleCumul());

            return;
        }
    }
}
